package com.example.administrator.puzzlepic.com.xiaoxu.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.Log;

import com.example.administrator.puzzlepic.R;

import java.util.List;

/**
 * Created by Administrator on 2016/4/7.
 */
public class ImageUtil {

    public static ItemBean mBlankItemBean;

    /**
     * 照片碎片加入集合
     * @param bitmapList 小位图的集合
     * @param itemList   封装切割后小图片的集合
     * @param type       x/y方向上切割的份数
     * @param  bitmap   要切割的图片
     *
     */

    public static Bitmap add2List(Bitmap bitmap, int type, List<Bitmap> bitmapList, List<ItemBean> itemList, Context context) {

        int smallBitmapWidth = bitmap.getWidth() / type;//每个小位图的尺寸
        int smallBitmapHeight = bitmap.getHeight() / type;

        Log.d("ImageUtil","smallBitmapWidth00"+ smallBitmapWidth + "smallBitmapHeight00"+smallBitmapHeight);

        for (int i = 1; i <= type; i++) {
            for (int j = 1; j <= type; j++) {

                Bitmap smallBitmap = Bitmap.createBitmap(bitmap, (j - 1) * smallBitmapWidth, (i - 1) * smallBitmapHeight,
                        smallBitmapWidth, smallBitmapHeight);

                Integer imgId=(i - 1) * type + j;
                Canvas can = new Canvas(smallBitmap);
                Paint paint = new Paint();

                paint.setStrokeWidth(3);
                paint.setTextSize(40);
                paint.setColor(Color.RED);
                paint.setTextAlign(Paint.Align.LEFT);
                can.drawText(imgId.toString(), 30,30, paint);
                can.save();

                bitmapList.add(smallBitmap);//将bitmap加入集合
                itemList.add(new ItemBean(imgId,
                        imgId, smallBitmap));   //将item对象加入集合

            }

        }
             //得到最后一个小位图
            bitmap = bitmapList.get(type * type - 1);

            bitmapList.remove(type * type - 1);
            itemList.remove(type * type - 1);
            //创建最后一个小位图

            Bitmap lastbitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.lastsmallbit);

            Bitmap changeLast = changePicSize(lastbitmap,smallBitmapWidth,smallBitmapHeight);//对最后一张小位图进行缩放处理

            bitmapList.add(changeLast);

            ItemBean itemBean = new ItemBean(type * type,type * type,changeLast);
            itemList.add(itemBean);

            mBlankItemBean = itemBean;


        return bitmap;//切掉的位图

    }

    /**
     * @param width 需求的图片宽
     *@param  height 需求的图片的高
     *@param bitmap 要改变的图片
     * */

    //改变图片的尺寸
   public static Bitmap changePicSize(Bitmap bitmap, float width, float height) {

        Matrix matrix = new Matrix();
        matrix.postScale(width / bitmap.getWidth(), height / bitmap.getHeight());

       //对bitmap的尺寸横、纵 都扩大指定的倍数，倍数由matrix封装
       //前四个参数决定着新图像由原图像那部分决定，第四个是matrix,第六个是新图像的宽高是否可变。
        Bitmap bm = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

        return bm;

    }
//
///**
// * @param from  要交换的
// * @param blank 空白图的
// * 交换对象中位图的位置
// * */
//    public static void changeBitmapPosition(ItemBean from,ItemBean blank){
//
//        if(from.getmBitmapId() == blank.getmBitmapId()){
//
//            return;
//        }
//
//        Log.d("ImageUtil",from.getmBitmapId()+ " from--blank" + blank.getmBitmapId());
//
//        ItemBean itemBean = new ItemBean();
//
//        itemBean.setmBitmapId(from.getmBitmapId());
//        itemBean.setmBitmap(from.getmBitmap());
//
//        from.setmBitmapId(blank.getmBitmapId());
//        from.setmBitmap(blank.getmBitmap());
//
//        blank.setmBitmapId(itemBean.getmBitmapId());
//        blank.setmBitmap(itemBean.getmBitmap());
//
//        mBlankItemBean = from;
//
//    }



    /**
     * 交换空格与点击Item的位置
     *
     * @param from  交换图
     * @param blank 空白图
     */
    public static void changeBitmapPosition(ItemBean from, ItemBean blank) {
        Log.d("ImageUtil",from.getmBitmapId()+ " from--blank" + blank.getmBitmapId());

        ItemBean tempItemBean = new ItemBean();
        // 交换BitmapId
        tempItemBean.setmBitmapId(from.getmBitmapId());
        from.setmBitmapId(blank.getmBitmapId());
        blank.setmBitmapId(tempItemBean.getmBitmapId());
        // 交换Bitmap
        tempItemBean.setmBitmap(from.getmBitmap());
        from.setmBitmap(blank.getmBitmap());
        blank.setmBitmap(tempItemBean.getmBitmap());
        // 设置新的Blank
        mBlankItemBean = from;
    }




}
